package com.huiquan.synonymy.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.EnhancedBaseDao;
import com.huiquan.synonymy.domain.SynonymyWord;
import com.ibatis.sqlmap.client.SqlMapClient;

@Repository
public class SynonymyWordDao extends EnhancedBaseDao<SynonymyWord> {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "SYNONYMY_WORD.";

	public SynonymyWordDao() {
		super(space);
	}

	@SuppressWarnings("unchecked")
	public List<String> retrieveWordListBySynonymyId(Long id) {
		Map<String, Object> param = new HashMap<>();
		param.put("synonymyId", id);
		return (List<String>) this.list(space + "RETRIEVE_WORD_LIST_BY_SYNONYMY_ID", param, sqlMapClient);
	}

	public void batchInsert(Long synonymyId, List<String> wordArray, User user, int type, String property) {
		Map<String, Object> param = new HashMap<>();
		param.put("type", type);
		param.put("synonymyId", synonymyId);
		param.put("property", property);
		param.put("wordArray", wordArray);
		param.put("userId", user.getUserId());
		param.put("userName", user.getRealName());

		this.insert(space + "BATCH_INSERT", param, sqlMapClient);
	}

	public void deleteBySynonymyId(Long synonymyId) {
		Map<String, Object> param = new HashMap<>();
		param.put("synonymyId", synonymyId);

		this.delete(space + "DELETE_BY_SYNONYMY_ID", param, sqlMapClient);
	}

	public void batchInsertBySynonymy(Integer type, String property) {
		Map<String, Object> param = new HashMap<>();
		param.put("type", type);
		param.put("property", property);

		this.delete(space + "BATCH_INSERT_BY_SYNONYMY", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> retrieveExistElementBySynonymyIds(String ids) {
		Map<String, Object> param = new HashMap<>();
		param.put("ids", ids);

		return (List<Map<String, Object>>) this.list(space + "RETRIEVE_EXIST_ELEMENT_BY_SYNONYMY_IDS", param,
				sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> retrieveNonExistDiBySynonymyIds(String ids) {
		Map<String, Object> param = new HashMap<>();
		param.put("ids", ids);

		return (List<Map<String, Object>>) this.list(space + "RETRIEVE_NON_EXIST_DI_BY_SYNONYMY_IDS", param,
				sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> retrieveWordListByWord(String word, int type, String property) {
		Map<String, Object> param = new HashMap<>();
		param.put("word", word);
		param.put("type", type);
		param.put("property", property);

		return (List<String>) this.list(space + "RETRIEVE_WORD_LIST_BY_WORD", param, sqlMapClient);
	}

	public void deleteWithoutSynonymy() {
		this.delete(space + "DLEETE_WITHOUT_SYNONYMY", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> retrieveRightOrReplaceVocabElementList(String property) {

		return (List<String>) this.list(space + "RETRIEVE_RIGHT_OR_REPLACE_VOCAB_ELEMENT_LIST", property, sqlMapClient);
	}

	public SynonymyWord retrieveDiSynonymyByName(String word) {
		return (SynonymyWord) this.object(space + "RETRIEVE_DI_SYNONYMY_BY_NAME", word, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<SynonymyWord> retrieveTSynonymyQuantization() {
		return (List<SynonymyWord>) this.list(space + "RETRIEVE_T_SYNONYMY_QUANTIZATION", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<SynonymyWord> retrieveByWordProperty(String property, String word) {
		Map<String, Object> param = new HashMap<>();
		param.put("word", word);
		param.put("property", property);
		return (List<SynonymyWord>) this.list(space + "RETRIEVE_BY_WORD", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, String>> retrieveElementSynonymByProperty(String property) {

		return (List<Map<String, String>>) this.list(space + "RETRIEVE_ELEMENT_SYNONYM_BY_PROPERTY", property,
				sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, String>> retrieveElementSynonymO() {
		return (List<Map<String, String>>) this.list(space + "RETRIEVE_ELEMENT_SYNONYM_O", sqlMapClient);
	}
	
	public void initOOaSynonym() {
		this.update(space + "INIT_O_OA_SYNONYM", sqlMapClient);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String, String>> retrieveSynonymyIdByWords(List<String> words,String property) {
		Map<String, Object> param = new HashMap<>();
		param.put("words", words);
		param.put("property", property);
		return (List<Map<String, String>>) this.list(space + "RETRIEVE_SYNONYMY_BY_WORDS", param, sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<SynonymyWord> checkRSynonymy() {
		return (List<SynonymyWord>) this.list(space + "CHECK_R_SYNONYMY",  sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<String> getStdByWord(Map<String, Object> param) {
		return (List<String>) this.list(space + "RETRIEVE_STD_BY_WORD",param , sqlMapClient);
	}
}
